<!-- 张梦莉  2017-11-06 商品属性组-->

<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model.trim="filters.attribute_group" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" v-on:click="getattributeslist">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="attributelist" highlight-current-row v-loading="listLoading" border
                  @selection-change="selsChange" style="width: 100%;">
            <!--<el-table-column type="selection" width="55">
            </el-table-column>-->
            <el-table-column prop="attribute_group" label="属性组名称">
            </el-table-column>
            <el-table-column prop="sort" label="排序">
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <!--<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
            <el-pagination layout="total, sizes, prev, pager ,next" :page-size="filters.pageSize"
                           @current-change="handleCurrentChange" @size-change="handleSizeChange" :total="total"
                           style="float:right;">
            </el-pagination>

        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="属性组名称" prop="attribute_group">
                    <el-input v-model="editForm.attribute_group" auto-complete="off" autofocus></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="editForm.sort" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="属性组名称" prop="attribute_group">
                    <el-input v-model="addForm.attribute_group" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="addForm.sort"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
  import {editAttributegroup, addAttributegroup, attributegroupList} from '../../api/api';

  export default {
    data() {
      return {
        filters: {
          pageSize: '10'
        },
        attributegroups: [],//商品属性组集合
        total: 0,
        page: 1,
        listLoading: false,
        sels: [],//列表选中列
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
        editFormRules: {
          attribute_group: [
            {required: true, message: '请输入姓名', trigger: 'blur'}
          ]
        },
        //编辑界面数据
        editForm: {
          "id": null,
          "attribute_group": "",
          "sort": "",
        },

        addFormVisible: false,//新增界面是否显示
        addLoading: false,
        addFormRules: {
          attribute_group: [
            {required: true, message: '请输入名称', trigger: 'blur'}
          ]
        },
        //新增界面数据
        addForm: {
          attribute_group: "",
          sort: "1",
        },
        timeout: null,

      }
    },
    computed: {},
    methods: {

      handleCurrentChange(val) {
        this.page = val;
        this.getattributesgrouplist();
      },
      //条数改变
      handleSizeChange(size) {
        this.filters.pageSize = size;
        this.getattributesgrouplist();
      },
      //获取服务器列表
      getattributesgrouplist() {
        let para = {
          page: this.page,
        };
        this.listLoading = true;
        //NProgress.start();
        attributegroupList(para).then((res) => {
          this.total = res.totalCount;
          this.attributelist = res;
          this.listLoading = false;
          //NProgress.done();
        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true;
        this.editForm = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd: function () {
        this.addFormVisible = true;
        this.addForm = {
          attribute_group: "",
          sort: null,
        };

      },
      //编辑
      editSubmit: function () {
        this.$refs.editForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.editForm);
              editAttributegroup(para).then((res) => {
                this.editLoading = false;
                //NProgress.done();
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
                this.editFormVisible = false;
                this.getattributesgrouplist();
              });
            });
          }
        });
      },
      //新增
      addSubmit: function () {
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.addLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.addForm);

              addAttributegroup(para).then((res) => {
                this.addLoading = false;
                //NProgress.done();
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['addForm'].resetFields();
                this.addFormVisible = false;
                this.getattributesgrouplist();
              });
            });
          }
        });
      },
    },
    mounted() {
      this.getattributesgrouplist();
//			console.log(this.$store.getters.getCount)
    },
    watch: {}
  }

</script>

<style scoped>

</style>
